package com.wfw.feichat.service.impl;

import com.wfw.feichat.entity.Chatmsg;
import com.wfw.feichat.mapper.ChatmsgMapper;
import com.wfw.feichat.service.ChatmsgService;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: wfw
 * @date: 2019/05/23
 */
@Service("chatmsgService")
public class ChatmsgServiceImpl implements ChatmsgService {
    @Autowired
    private ChatmsgMapper chatmsgMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Chatmsg getChatmsgById(String id) {
        return chatmsgMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public String addChatmsg(Chatmsg chatmsg) {
        String id = Sid.nextShort();
        Chatmsg chatmsg1 = getChatmsgById(id);
        while (chatmsg1 != null) {
            id = Sid.nextShort();
            chatmsg1 = getChatmsgById(id);
        }
        chatmsg.setId(id);
        chatmsgMapper.insertSelective(chatmsg);
        return id;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer modifyChatmsgByid(Chatmsg chatmsg) {
        if (chatmsg.getId() == null) {
            return 0;
        }
        return chatmsgMapper.updateByPrimaryKeySelective(chatmsg);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void batchUpdateMsgSigned(List<String> msgIdList) {
        chatmsgMapper.batchUpdateMsgSigned(msgIdList);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Chatmsg> getChatmsgListByAccepUid(String accepUid) {
        if (StringUtils.isBlank(accepUid)) {
            return null;
        }
        Chatmsg chatmsg = new Chatmsg();
        chatmsg.setAcceptUid(accepUid);
        chatmsg.setFlag(0);

        return chatmsgMapper.select(chatmsg);
    }
}
